﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Bottle.CommunityForum.Common
{
    public class LogHelper
    {
        public static Queue<string> ExceptionStringQueue = new Queue<string>();

        public static Action<string> WriterLogDal;

        static LogHelper()
        {
            WriterLogDal = (t) => { new Log4netWriter().LogWriterInfo(t); };

            ThreadPool.QueueUserWorkItem(o => {
                while (true)
                {
                    lock(ExceptionStringQueue)
                    {
                        if (ExceptionStringQueue.Count > 0)
                        {
                            string ex = ExceptionStringQueue.Dequeue();
                            WriterLogDal(ex);
                        }
                        else
                        {
                            Thread.Sleep(1000);
                        }
                    }
                }
            });
        }

        public static void WriteLog(string exceptionText)
        {
            lock (ExceptionStringQueue)
            {
                ExceptionStringQueue.Enqueue(exceptionText);
            }
        }
        
    }
}
